from rest_framework.views import APIView
from rest_framework.response import Response
from approval.models import Approval, ApprovalUser
from approval.serializer import ApprovalSer
from user.check_jwt import check_user, check_userjwt
from user.models import User, Role


# 查询审批
class ApprovalInfo(APIView):
    @check_user
    def get(self,request,payload):
        user_id = payload['id']
        user = User.objects.filter(id=user_id).first()
        if user.role_id_id == 2:
            approvals = Approval.objects.all()
            ser = ApprovalSer(approvals,many=True)  # 查询所有审批
            return Response({'code':200,'mag':ser.data})
        else:
            # todo 根据查询人的id查询自己发起的申请
            approvals = Approval.objects.filter(id=user_id).first()
            ser = ApprovalSer(approvals)
            return Response({'code':200,'msg':ser.data})

    def post(self, request):
        payload = check_userjwt(request)
        if not payload:
            return Response({"code": 401, "msg": "请登录"})
        user_name = payload.get("user_name")
        department = payload.get("department_id")
        approval_type_id = request.data.get("approval_type_id")
        approval_time = request.data.get("approval_time")

        appro = Approval.objects.create(user=user_name, approval_type_id=approval_type_id, approval_time=approval_time,
                                        status=3, now_approval_id=1, department_id=department)
        appro.save()

        return Response({"code": 200, "msg": "成功"})

    def delete(self, request, approval_id):
        payload = check_userjwt(request)
        if not payload:
            return Response({"code": 401, "msg": "请登录"})
        Approval.objects.filter(id=approval_id).delete()
        return Response({"code": 200, "msg": "删除成功"})


# 待办
class ApprovalBackView(APIView):
    def get(self,request):
        payload = check_userjwt(request)
        if not payload:
            return Response({"code":401,"msg":"请登录"})
        user_id = payload.get("id")
        user = User.objects.filter(id=user_id).first()
        if user.role_id_id == 1:
            approvals = Approval.objects.filter(status=3).all()
            ser = ApprovalSer(approvals, many=True)  # 查询待审批
            return Response({'code': 200, 'mag': ser.data})
        else:
            # todo 根据查询人的id查询自己发起的申请
            approvals = Approval.objects.filter(id=user_id).first()
            ser = ApprovalSer(approvals)
            return Response({'code': 200, 'msg': ser.data})


# 已办
class ApprovalDoneView(APIView):
    def get(self,request):
        payload = check_userjwt(request)
        if not payload:
            return Response({"code":401,"msg":"请登录"})
        user_id = payload.get("id")
        user = User.objects.filter(id=user_id).first()
        if user.role_id_id == 1:
            approvals = Approval.objects.filter(status=1).all()
            ser = ApprovalSer(approvals, many=True)  # 查询待审批
            return Response({'code': 200, 'mag': ser.data})
        else:
            # todo 根据查询人的id查询自己发起的申请
            approvals = Approval.objects.filter(id=user_id).first()
            ser = ApprovalSer(approvals)
            return Response({'code': 200, 'msg': ser.data})


# 审核流程 用户提交之后开始审核
class ApprovalUserInfo(APIView):
    @check_user
    def get(self, request,payload):
        user_id = payload['id']  # 判断用户是否登录

        user_role = Role.objects.get()  # 所有角色

        if user_role.role_name == '超级管理员':
            appro = Approval.objects.all()
            ser = ApprovalSer(appro,many=True)
            return Response({'msg':ser.data})
        elif user_role.role_name == '员工':
            appro = Approval.objects.filter(id=user_id).first()
            ser = ApprovalSer(appro)
            return Response({'msg':ser.data})

        # user = User.objects.filter(id=user_id).first()  # 获取登录的用户信息
        # appuser = Approval.objects.filter(user_id=user.id)
        # user_approval = Approval.objects.all()  # 所有审批内容
        #
        # for i in user_approval:
        #     approve_user_num = i.now_approval.num_user  # 第几级审批人
        #     if approve_user_num == 1:
        #         # now_approval = 1
        #         Approval.objects.update(now_approval=1)
        # else:
        #     # todo
        #     approve_num_user = int(user.role_id_id) - 1  # 发起请假的审批级别 减1
        #     print('11111111111111111111111',type(approve_num_user),approve_num_user)
        #
        #     # print('22222222222222222222222222222222',user.role_id_id)
        #     # list = []
        #     appro = Approval.objects.filter(now_approval=approve_num_user).all()
        #     # for i in appro:
        #     #     a = {
        #     #         'name':i.now_approval
        #     #     }
        #     #     list.append(a)
        #     ser = ApprovalSer(appro,many=True)
        #     return Response({'code':200,'msg':ser.data})




from django.conf import settings
from django.core.mail import send_mail
from django.http import HttpResponse



def send_email(request):
    subject = 'C语言中文网链接'  # 主题
    from_email = settings.EMAIL_FROM  # 发件人，在settings.py中已经配置
    to_email = 'xxxxx@qq.com'  # 邮件接收者列表
    # 发送的消息
    message = 'c语言中文网欢迎你点击登录 http://c.biancheng.net/'  # 发送普通的消息使用的时候message
    # meg_html = '<a href="http://www.baidu.com">点击跳转</a>'  # 发送的是一个html消息 需要指定
    send_mail(subject, message, from_email, [to_email])
    return HttpResponse('OK,邮件已经发送成功!')



